package com.xiangwei.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xiangwei.dao.GodMapper;
import com.xiangwei.dao.ProductMapper;
import com.xiangwei.dao.QrcodeMapper;
import com.xiangwei.dao.ScanRecordMapper;
import com.xiangwei.entity.Product;
import com.xiangwei.service.IQrcodeService;
import com.xiangwei.util.Common;

@Service
public class QrcodeServiceImpl implements IQrcodeService{

	@Resource
	private QrcodeMapper qrcodeMapper;
	@Resource
	private ProductMapper productMapper;
	@Resource
	private ScanRecordMapper scanRecordMapper;
	@Resource
	private GodMapper godMapper;
	
	@Transactional
	public Long insert(Map<String, Object> barcode) {
//		qrcodeMapper.insert(barcode);
		return (Long) barcode.get("qrcodeId");
	}

	@Transactional
	public int existTable(String tableName) {
		return qrcodeMapper.existTable(tableName);
	}

	@Transactional
	public int createNewTable(String tableName) {
		return qrcodeMapper.createNewTable(tableName);
	}

	@Override
	public int getMaxId(Map<String, Object> qrcode) {
		return  qrcodeMapper.getMaxId(qrcode);
	}

	@Override
	public void bathchInsert(Map<String, Object> map) {
		qrcodeMapper.bathchInsert(map);
	}

	@Override
	public List<Integer> getIdList(Map<String, Object> qrcode) {
		return qrcodeMapper.getIdList(qrcode);
	}

	@Override
	public int createScanrecordTable(String id) {
		return qrcodeMapper.createScanrecordTable(id);
	}

	@Override
	public int createGodTable(String tableName) {
		return qrcodeMapper.createGodTable(tableName);
	}

	  @Override
		public int getQrcodeCount() {
			int count = 0;
			Map<String,Object> map = new HashMap<String, Object>();
			List<Product> list = productMapper.getAllProduct();
			if(Common.isNullOrEmpty(list) || list.size() == 0){
				return count;
			}
			for(Product p : list){
				String tableName = "qlm_fcrm_qrcode_"+p.getId();
				if(qrcodeMapper.existTable(tableName)>0){
					map.put("productId", p.getId());
					count += qrcodeMapper.getMaxId(map);
				}
			}
			return count;
		}

	@Override
	public int getScanrecordCount() {
		int count = 0;
		Map<String,Object> map = new HashMap<String, Object>();
		List<Product> list = productMapper.getAllProduct();
		if(Common.isNullOrEmpty(list) || list.size() == 0){
			return count;
		}
		for(Product p : list){
			String tableName = "qlm_portal_scanrecord_"+p.getId();
			if(qrcodeMapper.existTable(tableName)>0){
				map.put("productId", p.getId());
				count += scanRecordMapper.getScanrecordCount(map);
			}
		}
		return count;
	}

	@Override
	public int getGodCount() {
		int count = 0;
		Map<String,Object> map = new HashMap<String, Object>();
		List<Product> list = productMapper.getAllProduct();
		if(Common.isNullOrEmpty(list) || list.size() == 0){
			return count;
		}
		for(Product p : list){
			String tableName = "qlm_portal_god_"+p.getId();
			if(qrcodeMapper.existTable(tableName)>0){
				map.put("productId", p.getId());
				count += godMapper.getGodCount(map);
			}
		}
		return count;
	}

	@Override	
	public boolean qrcodeIsExist(Map<String, Object> map) {
		
		String productId = (String) map.get("productId");
		if(!Common.isEmptyString(productId)){//productId为空
			return false;
		}
		Product p = productMapper.selectByPrimaryKey(Integer.parseInt(productId));
		if(Common.isNullOrEmpty(p)){//产品表中不存在此productId
			return false;
		}		
		String tableName = "qlm_fcrm_qrcode_"+productId;
		if(qrcodeMapper.existTable(tableName) > 0 && qrcodeMapper.selectQrcodeCount(map) == 1){
			return true;
		}		
		return false;
	}

}
